﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.PlugIn;
using EPiServer;
using System.Web;
using EPiServer.Security;
using EPiServer.Core;
using EPiServer.Configuration;
using System.Configuration;
using EPiServer.Personalization;


namespace EPiServer.Research.VirtualRoles
{
    public class VirtualRolesInit : PlugInAttribute
    {
        private static bool DoPageView = false;
        private static bool DoScoring = false;

        private static List<string> ScoreNames = new List<string>();

        public static void Start()
        {
            //If needed, attach to event-handlers.
            
            var providers=EPiServerSection.Instance.VirtualRoles.Providers.Cast<ProviderSettings>();

            //If we have a PageView Provider then enable it
            if (providers.Where(ps => ps.Type.Contains(typeof(PageViewRole).FullName)).Count() > 0)
                DoPageView = true;

            //If we have scoring providers, then enable them
            if (providers.Where(ps => ps.Type.Contains(typeof(ScoreRole).FullName)).Count() > 0)
            {
                DoScoring = true;
                ScoreNames.AddRange(providers.Where(ps => ps.Type.Contains(typeof(ScoreRole).FullName)).Select(ps => ps.Parameters["scorename"]));
            }

            PageBase.PageSetup += new PageSetupEventHandler(PageBase_PageSetup);
        }

        static void PageBase_PageSetup(PageBase sender, PageSetupEventArgs e)
        {
            sender.Load += new EventHandler(sender_Load);
        }

        static void sender_Load(object sender, EventArgs e)
        {
            if (sender is PageBase)
            {
                var lnk = (sender as PageBase).CurrentPageLink;
                if (!PageReference.IsNullOrEmpty(lnk))
                {
                    if (DoPageView)
                    {
                        //Add to session - if session scope.
                        var pb = (sender as PageBase);
                        if (pb.Session["PageViews"] == null) pb.Session.Add("PageViews", new List<string>());
                        var lst = pb.Session["PageViews"] as List<string>;
                        if (!lst.Contains(lnk.ToString())) lst.Add(lnk.ToString());
                    }
                    //todo - if user scope - add to cookie.

                    //Scoring
                    if (DoScoring)
                    {
                        var pd = (sender as PageBase).CurrentPage;
                        if (pd != null)
                        {
                            foreach (string s in ScoreNames)
                            {
                                object o = pd[s];
                                if (o != null)
                                {
                                    if (o is int)
                                    {
                                        EPiServerProfile.Current.SetPropertyValue(s, ((int)EPiServerProfile.Current[s]) + ((int)o));
                                        EPiServerProfile.Current.Save();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
